package com.emergency.evaluate.controller;


import com.emergency.common.entity.Result;
import com.emergency.common.entity.StatusCode;

import com.emergency.evaluate.domain.Extract;
import com.emergency.evaluate.domain.Indicator;
import com.emergency.evaluate.domain.Weight;
import com.emergency.evaluate.service.ExtractService;
import com.emergency.evaluate.service.IndicatorService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/3/10.
 *
 */
@RestController
@RequestMapping("/indicator")
public class IndicatorController {
   @Autowired
    private IndicatorService indicatorService;

   @Autowired
    private ExtractService extractService;

    /*添加或修改指标 */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result Add(@RequestBody List<Indicator> indicatorList){
        List <Indicator> indicators = indicatorService.add ( indicatorList );
        return new Result ( true, StatusCode.OK,  "操作成功" ,indicators);
    }

    //选择不同指标类型进行指标库查询
    @RequestMapping(value = "/list/{kind}",method = RequestMethod.POST)
    public Result check(@PathVariable ("kind")Integer kind){
        List <Indicator> indicatorLibraries = indicatorService.findbykind (kind);
        return  new Result ( true, StatusCode.OK,  "操作成功", indicatorLibraries );
    }

    //录入或修改权重，录入需要输入指标ID和想定ID及weight，修改须输入weightID和weight
    @RequestMapping(value = "/weight/add",method = RequestMethod.POST)
    public Result addWeight(@RequestBody List<Weight> weightList){
        List <Weight> weights = indicatorService.addWeight ( weightList );
        return new Result ( true,StatusCode.OK,"操作成功",weights );
    }

    //指标分页展示
    @RequestMapping(value = "/{page}/{size}",method = RequestMethod.POST)
    public Result list(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){
        Page <Indicator> indicatorPage = indicatorService.findList ( searchMap, page, size );
        return new Result ( true, StatusCode.OK,  "操作成功", indicatorPage );
    }

    //根据指标id删除指标
    @RequestMapping(value = "/del/{id}",method = RequestMethod.POST)
    public Result Delete(@PathVariable ("id") Long id ){
        indicatorService.deleteById ( id );
        return new Result ( true, StatusCode.OK,  "操作成功" );
    }

    //添加或修改提取指标表
    @RequestMapping(value = "/extract/save/{scenarId}",method = RequestMethod.POST)
    public Result ExtractSave(@RequestBody List<Extract> extractList,@PathVariable long scenarId){
        List <Extract> extracts = extractService.save ( scenarId, extractList );
        return new Result ( true, StatusCode.OK,  "操作成功", extracts);
    }

    //根据想定编号查询提取的指标
    @RequestMapping(value = "/extract/{id}",method = RequestMethod.POST)
    public Result extract(@PathVariable long id){
        List <Extract> extractList = extractService.findAllBySid ( id );
        return new Result ( true, StatusCode.OK,  "操作成功", extractList );
    }
    //根据提取指标编号删除临时表上提取的指标
    @RequestMapping(value = "/extract/del/{id}",method = RequestMethod.POST )
    public Result ExtractDelete(@PathVariable Long id){
       extractService.deleteById (id);
        return new Result ( true, StatusCode.OK,  "操作成功" );
    }


    //用户修改权重
    @RequestMapping(value = "/weight/add/{kind}",method = RequestMethod.POST )
    public Result WeightAdd(@PathVariable Integer kind,@RequestBody Double[][] matrix){
        List <Extract> extractList = extractService.add ( kind, matrix );
        return new Result ( true, StatusCode.OK,  "操作成功" ,extractList);
    }
}

